package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.ui.VCodeButton;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;

public class RegisterActivity extends BaseAppCompatActivity implements RequestListener
{
    private static final String TAG = RegisterActivity.class.getSimpleName();
    Button   register_next_bt;
    EditText phonenumber_et;
    EditText entercode_et;
    // 计时任务
    TimerTask task;
    Gson gson = new Gson();
    // 获取验证码
    VCodeButton obtain_tv;
    private Timer timer = new Timer();
    // 短信倒计时时间
    private int   time  = 10;
    private CheckBox agreement;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        phonenumber_et = (EditText) findViewById(R.id.phonenumber_et);
        entercode_et = (EditText) findViewById(R.id.entercode_et);
        obtain_tv = (VCodeButton) findViewById(R.id.obtain_tv);
        register_next_bt = (Button) findViewById(R.id.register_next_bt);
        agreement = (CheckBox) findViewById(R.id.protocol_cb);
        Utils.initTitle("获取验证码", RegisterActivity.this, true);
        obtain_tv.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                doRegisterForPost();
            }
        });
        register_next_bt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (agreement.isChecked() == false)
                {
                    LoanToast.showToastShort("请认可黄金屋协议！", RegisterActivity.this);
                    return;
                }
                doVerifiedCode();

            }
        });
    }

    /**
     * 使用异步处理手机是否注册
     */

    private void doRegisterForPost()
    {
        RequestMethod.getMethod().doCellphoneIsRegistered(phonenumber_et.getText().toString(), RegisterActivity.this, RegisterActivity.this);
    }

    /**
     * 使用异步处理获取验证码
     */

    private void doSendVerifiedCode()
    {
        RequestMethod.getMethod().doSendVerifiedCode(phonenumber_et.getText().toString(), RegisterActivity.this, RegisterActivity.this);
    }

    /**
     * 判断验证吗是否正确
     */
    private void doVerifiedCode()
    {
        RequestMethod.getMethod().doVerifyCode(phonenumber_et.getText().toString(), entercode_et.getText().toString(), RegisterActivity.this, RegisterActivity.this);
    }

    void smsIsOK()
    {
        // 则则说明短信发送成功
        Toast.makeText(RegisterActivity.this, "短信发送成功",
            Toast.LENGTH_SHORT).show();
        //开始计时
        obtain_tv.startCount();
    }

    @Override
    public void requestSuccess(String tag, String message)
    {
        switch (tag)
        {
        case RequestTag.APPCELLPHONEISREGISTERED:
            //手机号可用调用获取验证码
            doSendVerifiedCode();
            break;
        case RequestTag.APPSENDVERIFIEDCODE:
            //发送验证码成功
            //启动倒计时
            smsIsOK();
            break;
        case RequestTag.APPVERIFYCODE:
            //判断验证码是否正确
            LoanToast.showToastShort("校验成功", RegisterActivity.this);
            Intent i = new Intent(RegisterActivity.this, RegisterActivity02.class);
            i.putExtra("phonenumber", phonenumber_et.getText().toString());
            startActivity(i);
            finish();
            break;

        }
    }

    @Override
    public void requestError(String message)
    {
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), RegisterActivity.this);
    }
}
